package com.vhall.component.plugin.common.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: rui.zhou
 * @date 2021/8/14 16:54
 */
public class ListUtils {

    public static boolean isDiff(List<Integer> list1, List<Integer> list2) {

        if (list1.size() != list2.size()) {
            return false;
        }
        if (!list1.containsAll(list2)) {
            return false;
        }
        return true;
    }


    public static <T> List<List<T>> spiltList(List<T> source, int n) {
        List<List<T>> result = new ArrayList<>();
        //(先计算出余数)
        int remainder = source.size() % n;
        //然后是商
        int number = source.size() / n;
        //偏移量
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

    public static String listToString(List list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }
}
